<?php

include 'matakuliah_controller.php';
include 'perkuliahan_controller.php';

if ($_GET['action'] == "viewByFilter") {
    $json = $HTTP_RAW_POST_DATA;
    if ($json != NULL) {
        echo viewByFilter($json);
    } else {
        echo 'Invalid Request';
    }
} else if ($_GET['action'] == "add") {
    $json = $HTTP_RAW_POST_DATA;
    if ($json != NULL) {
        if (createPerkuliahan($json))
            echo 'Create Success.';
        else
            echo 'Create Failed!';
    } else {
        echo 'Invalid Request';
    }
} else if ($_GET['action'] == "update") {
    $json = $HTTP_RAW_POST_DATA;
    if ($json != NULL) {
        if (updatePerkuliahan($json))
            echo 'Update Success.';
        else
            echo 'Update Failed!';
    } else {
        echo 'Invalid Request';
    }
} else if ($_GET['action'] == "delete") {
    $json = $HTTP_RAW_POST_DATA;
    if ($json != NULL) {
        if (deletePerkuliahan($json))
            echo 'Delete Success.';
        else
            echo 'Delete Failed!';
    } else {
        echo 'Invalid Request';
    }
} else if ($_GET['action'] == "viewPerkuliahanDosen") {
    $json = $HTTP_RAW_POST_DATA;
    if ($json != NULL) {
        echo viewPerkuliahanDosen($json);
    } else {
        echo viewPerkuliahanDosen(NULL);
    }
} else if ($_GET['action'] == "viewPerkuliahanMhs") {
    $json = $HTTP_RAW_POST_DATA;
    if ($json != NULL) {
        echo viewPerkuliahanMhs($json);
    } else {
        echo 'Invalid Request';
    }
} else if ($_GET['action'] == "viewJoinedPerkuliahan") {
    $json = $HTTP_RAW_POST_DATA;
    if ($json != NULL) {
        echo viewJoinedPerkuliahan($json);
    } else {
        echo 'Invalid Request';
    }
} else if ($_GET['action'] == "find") {
    $json = $HTTP_RAW_POST_DATA;
    if ($json != NULL) {
        echo findPerkuliahan($json);
    } else {
        echo 'Invalid Request';
    }
} else if ($_GET['action'] == "view") {
    echo viewMatkul();
} else if ($_GET['action'] == "joinKuliah") {
    $perkuliahan = $HTTP_RAW_POST_DATA;
    if ($perkuliahan != NULL) {
        $json = json_decode($perkuliahan, false);

        if (joinRequest($json) > 0) {
            echo 'Request already sent, wait for accepted by your dosen';
        } else if (alreadyJoined($json) > 0) {
            echo 'You are already joined for ' . $json->{'DetMatkul'}->{'Matkul'}->{'Nama'};
        } else {
            if (joinKuliah($json))
                echo 'Join request for ' . $json->{'DetMatkul'}->{'Matkul'}->{'Nama'} . " was sending";
            else
                echo 'Request not completed.';
        }
    } else {
        echo 'Invalid Request';
    }
} else if ($_GET['action'] == "leave") {
    $perkuliahan = $HTTP_RAW_POST_DATA;
    if ($perkuliahan != NULL) {
        $json = json_decode($perkuliahan, false);
        if (leavePerkuliahan($json))
            echo 'Success meninggalkan kuliah ' . $json->{'DetMatkul'}->{'Matkul'}->{'Nama'} . ".";
        else
            echo 'Gagal meninggalkan kuliah ' . $json->{'DetMatkul'}->{'Matkul'}->{'Nama'} . "!";
    } else {
        echo 'Invalid Request';
    }
} else if ($_GET['action'] == "listJoined") {
    $json = $HTTP_RAW_POST_DATA;
    if ($json != NULL) {
        echo viewJoinedMember($json);
    } else {
        echo 'Invalid Request';
    }
} else if ($_GET['action'] == "listPending") {
    $json = $HTTP_RAW_POST_DATA;
    if ($json != NULL) {
        echo viewPendingMember($json);
    } else {
        echo 'Invalid Request';
    }
} else if ($_GET['action'] == "acceptPendingByNim") {
    $matkul = $HTTP_RAW_POST_DATA;
    if ($matkul != NULL) {
        $json = json_decode($matkul, false);
        if (acceptPendingMemberByNim($json))
            echo 'Mahasiswa with NIM ' . $json->{'Mhs'}->{'Nim'} . ' was accepted to joined to ' . $json->{'DetMatkul'}->{'Matkul'}->{'Nama'};
        else
            echo 'Request not completed.';
    } else {
        echo 'Invalid Request';
    }
} else if ($_GET['action'] == "acceptPending") {
    $matkul = $HTTP_RAW_POST_DATA;
    if ($matkul != NULL) {
        $json = json_decode($matkul, false);
        if (acceptPendingMember($json))
            echo 'Pendaftaran Mahasiswa ke kuliah ' . $json->{'DetMatkul'}->{'Matkul'}->{'Nama'} . ' telah disetujui';
        else
            echo 'Request not completed.';
    } else {
        echo 'Invalid Request';
    }
} else if ($_GET['action'] == "rejectPendingByNim") {
    $matkul = $HTTP_RAW_POST_DATA;
    if ($matkul != NULL) {
        $json = json_decode($matkul, false);
        if (rejectPendingMemberByNim($json))
            echo 'Mahasiswa with NIM ' . $json->{'Mhs'}->{'Nim'} . ' was rejected to joined to ' . $json->{'DetMatkul'}->{'Matkul'}->{'Nama'};
        else
            echo 'Request not completed.';
    } else {
        echo 'Invalid Request';
    }
} else if ($_GET['action'] == "rejectPending") {
    $matkul = $HTTP_RAW_POST_DATA;
    if ($matkul != NULL) {
        $json = json_decode($matkul, false);
        if (rejectPendingMember($json))
            echo 'Pendaftaran Mahasiswa dari kuliah ' . $json->{'DetMatkul'}->{'Matkul'}->{'Nama'} . ' telah di batalkan';
        else
            echo 'Request not completed.';
    } else {
        echo 'Invalid Request';
    }
} else if ($_GET['action'] == "viewNilaiMember") {
    $json = $HTTP_RAW_POST_DATA;
    if ($json != NULL) {
        echo viewNilaiMember($json);
    } else {
        echo 'Invalid Request';
    }
} else if ($_GET['action'] == "updateNilaiMember") {
    $matkul = $HTTP_RAW_POST_DATA;
    if ($matkul != NULL) {
        $json = json_decode($matkul, false);
        if (updateNilaiMember($json))
            echo 'Nilai Mahasiswa with NIM ' . $json->{'Mhs'}->{'Nim'} . ' saved';
        else
            echo 'Request not completed.';
    } else {
        echo 'Invalid Request';
    }
} else if ($_GET['action'] == "importNewKuliah") {
    $data = $HTTP_RAW_POST_DATA;
    if ($data != NULL) {
        $json = json_decode($data, true);
		
        if (importKuliah($json, true))
            echo 'Data success imported.';
        else
            echo 'Request not completed.';
	} else {
        echo 'Invalid Request';
    }
} else if ($_GET['action'] == "importMemberKuliah") {
    $data = $HTTP_RAW_POST_DATA;
    if ($data != NULL) {
        $json = json_decode($data, true);
		
        if (importKuliah($json, false))
            echo 'Data Success imported.';
        else
            echo 'Request not completed.';
	} else {
        echo 'Invalid Request';
    }
} else {
    echo 'Action not defined!';
}

?>
